//
// Created by Snow on 2023/4/9.
//
//深度优先遍历算法
#include"graph0.cpp"
int visited[MAXV]={0};//全局数组
void DFS(AdjGraph *G,int v)
{
    ArcNode *p;
    visited[v]=1;//置已访问标记
    printf("%d ",v);//输出被访问顶点的编号
    p=G->adjlist[v].firstarc;//p指向顶点v的第一个邻接点
    while(p!=nullptr)
    {
        if(visited[p->adjvex]==0)//若p->adjvex顶点未被访问，递归访问它
            DFS(G,p->adjvex);
        p=p->nextarc;//p指向顶点v的下一个邻接点
    }
}
int main()
{
    AdjGraph *G;
    int A[MAXV][MAXV]={{0,1,0,1,1},{1,0,1,1,0},
                       {0,1,0,1,1},{1,1,1,0,1},{1,0,1,1,0}};
    int n=5, e=8;
    CreateAdj(G,A,n,e);			//建立《教程》中图8.1(a)的邻接表
    printf("AdjGraph:\n");
    DispAdj(G);					//输出邻接表G
    printf("DFS: ");
    DFS(G,2);
    printf("\n");
    DestroyAdj(G);				//销毁邻接表
    return 1;
}
